# -*- coding: utf-8 -*-
# @Time: 2022/9/9 22:26
# @File: bird.py
import pygame
from util import animation_surfaces
from util import image_surface
from setting import *


class Bird(pygame.sprite.Sprite):

    def __init__(self, pipe_sprites):
        super().__init__()
        self.pipe_sprites = pipe_sprites
        self.screen = pygame.display.get_surface()

        # 导入动画和图像
        self.red_surfaces = animation_surfaces(
            "../material/animation/bird_red_fly", False)
        self.blue_surfaces = animation_surfaces(
            "../material/animation/bird_blue_fly", False)
        self.dead_surface = image_surface(
            "../material/image/bird_blue_death.png", False)
        self.surface = self.blue_surfaces

        # 设置矩形框大小和宽高
        self.rect = pygame.Rect(
            (SCREEN_WIDTH - BIRD_IMAGE_WIDTH) // 2,
            (SCREEN_HEIGHT - BIRD_IMAGE_HEIGHT) // 2,
            BIRD_IMAGE_WIDTH, BIRD_IMAGE_HEIGHT
        )
        self.position = list(self.rect.center)  # 中点的坐标

        # 播放帧相关
        self.frame_index = 0
        self.frame_number = len(self.blue_surfaces)

        # 鸟的速度相关参数
        self.speed = 0
        self.up_speed = UP_SPEED
        self.down_acceleration = GRAVITY

        self.font1 = pygame.font.Font("../material/font/Boxy-Bold.ttf", 50)
        self.font2 = pygame.font.Font("../material/font/Boxy-Bold.ttf", 20)
        self.alive = True
        self.strengthen = False

    def fly_animation(self, duration_time):
        """
        飞行动画
        :param duration_time: 间隔时间
        :return:
        """
        # 计算是第几帧
        self.frame_index += BIRD_PLAY_SPEED * duration_time
        if self.frame_index >= self.frame_number:
            self.frame_index = 0
        # 判断鸟是否强化
        if self.strengthen:
            self.surface = self.red_surfaces[int(self.frame_index)]
        else:
            self.surface = self.blue_surfaces[int(self.frame_index)]
        # 绘制动画，blit方法需要传入左上角的坐标
        self.screen.blit(self.surface, self.rect.topleft)

    def move(self, duration_time):
        """
        鸟的移动和操控
        :param duration_time:
        :return:
        """
        # 如果死亡，掉落到屏幕下方不动
        if not self.alive and self.rect.y >= SCREEN_HEIGHT:
            self.rect.y = SCREEN_HEIGHT

        # 输入检测
        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE] and self.alive:
            self.speed = self.up_speed  # 向上飞
        if keys[pygame.K_RETURN]:  # 复活重置参数
            self.speed = 0
            self.position[1] = (SCREEN_HEIGHT - BIRD_IMAGE_HEIGHT) // 2
            self.alive = True
        else:  # 下落速度计算
            self.speed += self.down_acceleration * duration_time
        # 鸟位置更新
        self.position[1] += self.speed * duration_time
        self.rect.y = self.position[1]

    def test_move(self, duration_time):
        """
        测试模式下的移动，通过上下键直接移动
        :param duration_time: 间隔时间
        :return:
        """
        key = pygame.key.get_pressed()
        if key[pygame.K_UP]:
            self.position[1] -= 300 * duration_time
        if key[pygame.K_DOWN]:
            self.position[1] += 300 * duration_time
        self.rect.y = self.position[1]

    def check_collision(self):
        """
        检测与水管、边界的碰撞情况
        :return:
        """
        for sprite in self.pipe_sprites:
            if (self.rect.x - PIPE_IMAGE_WIDTH < sprite.x <
                    self.rect.x + PIPE_IMAGE_WIDTH):  # 鸟附近的水管才做碰撞检测
                bird_rect = self.rect.inflate(0, -10)  # 鸟缩小碰撞体积
                if bird_rect.collidelistall(sprite.collision_area):
                    self.alive = False

        if self.rect.y < 0 or self.rect.y > (SCREEN_HEIGHT - BIRD_IMAGE_HEIGHT):
            self.alive = False

    def update(self, duration_time):
        """
        鸟的更新
        :param duration_time: 间隔时间
        :return:
        """
        self.move(duration_time)
        if self.alive:
            self.fly_animation(duration_time)
        else:
            # 鸟切换为死亡动画
            self.screen.blit(self.dead_surface, self.rect.topleft)

            # 屏幕绘制结束文字
            font_surface = self.font1.render("BIRD IS DEAD", False, "yellow")
            font_rect = font_surface.get_rect(
                center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
            self.screen.blit(font_surface, font_rect.topleft)

            # 屏幕绘制按回车继续的说明
            font_surface = self.font2.render(
                "Press enter to continue", False, "black")
            font_rect = font_surface.get_rect(center=(
                SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + BIRD_IMAGE_HEIGHT * 2))
            self.screen.blit(font_surface, font_rect.topleft)

        self.check_collision()
